package com.hzcx.app.simplechat.ui.friend;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMMessage;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.chat.event.ChatShareUserEvent;
import com.hzcx.app.simplechat.ui.friend.adapter.FriendSearchAdapter;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.friend.contract.FriendSearchContract;
import com.hzcx.app.simplechat.ui.friend.event.FriendSearchClickEvent;
import com.hzcx.app.simplechat.ui.friend.presenter.FriendSearchPresenter;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 搜索通讯录
 */
public class FriendSearchActivity extends BaseActivity<FriendSearchPresenter> implements FriendSearchContract.View {

    public static final String INTENT_TAG = "INTENT_TAG";

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.rv_friend)
    RecyclerView rvFriend;

    private List<FriendCityBean> friendData;
    private FriendSearchAdapter searchAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_friend_search;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FriendSearchPresenter();
    }

    @Override
    public void initView() {

        // 弹出软键盘
        AppHelper.showSoftKeyboard(etSearch, this);

        tvTips.setText("搜索昵称/" + getResources().getString(R.string.app_name) + "号/备注名/描述");
        friendData = new ArrayList<>();
        searchAdapter = new FriendSearchAdapter(friendData);
        rvFriend.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFriend.setAdapter(searchAdapter);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etSearch.getText().toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                    mPresenter.searchFriendList(FriendSearchActivity.this, etSearch.getText().toString());
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                tvTips.setVisibility(View.VISIBLE);
                friendData.clear();
                searchAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    if (EmptyUtils.isEmpty(etSearch.getText().toString())) {
                        return false;
                    }
                    if (etSearch.getText().toString().length() > 0) {
                        ivClear.setVisibility(View.VISIBLE);
                        mPresenter.searchFriendList(FriendSearchActivity.this, etSearch.getText().toString());
                    } else {
                        ivClear.setVisibility(View.GONE);
                    }
                    tvTips.setVisibility(View.VISIBLE);
                    friendData.clear();
                    searchAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        searchAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (getIntent().getIntExtra(INTENT_TAG, 0) == 1) {
                startActivity(new Intent(FriendSearchActivity.this, UserInfoActivity.class)
                        .putExtra(UserInfoActivity.INTENT_USER_ID, friendData.get(position).getMember_id()));
            } else {
                EventBus.getDefault().post(new ChatShareUserEvent(getIntent().getIntExtra(INTENT_TAG, 0), friendData.get(position)));
                finish();
            }
        });

    }

    /**
     * 好友搜索成功
     *
     * @param data
     */
    @Override
    public void searchResult(List<FriendCityBean> data) {
        if (data == null || data.size() <= 0) {
            showError("未搜素到相关用户");
            return;
        }
        tvTips.setVisibility(View.GONE);
        friendData.addAll(data);
        searchAdapter.notifyDataSetChanged();
    }
}
